<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>simpleobservable.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_observe_simpleobservable.js.html">simpleobservable.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview An implementation of {@code Observable} that can be
<a name="line17"></a> * used as base class or composed into another class that wants to
<a name="line18"></a> * implement {@code Observable}.
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.observe.SimpleObservable&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line24"></a>goog.require(&#39;goog.array&#39;);
<a name="line25"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line26"></a>goog.require(&#39;goog.labs.observe.Notice&#39;);
<a name="line27"></a>goog.require(&#39;goog.labs.observe.Observable&#39;);
<a name="line28"></a>goog.require(&#39;goog.labs.observe.Observer&#39;);
<a name="line29"></a>goog.require(&#39;goog.object&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * A simple implementation of {@code goog.labs.observe.Observable} that can
<a name="line35"></a> * be used as a standalone observable or as a base class for other
<a name="line36"></a> * observable object.
<a name="line37"></a> *
<a name="line38"></a> * When another class wants to implement observable without extending
<a name="line39"></a> * {@code SimpleObservable}, they can create an instance of
<a name="line40"></a> * {@code SimpleObservable}, specifying {@code opt_actualObservable},
<a name="line41"></a> * and delegate to the instance. Here is a trivial example:
<a name="line42"></a> *
<a name="line43"></a> * &lt;pre&gt;
<a name="line44"></a> *   ClassA = function() {
<a name="line45"></a> *     goog.base(this);
<a name="line46"></a> *     this.observable_ = new SimpleObservable(this);
<a name="line47"></a> *     this.registerDisposable(this.observable_);
<a name="line48"></a> *   };
<a name="line49"></a> *   goog.inherits(ClassA, goog.Disposable);
<a name="line50"></a> *
<a name="line51"></a> *   ClassA.prototype.observe = function(observer) {
<a name="line52"></a> *     this.observable_.observe(observer);
<a name="line53"></a> *   };
<a name="line54"></a> *
<a name="line55"></a> *   ClassA.prototype.unobserve = function(observer) {
<a name="line56"></a> *     this.observable_.unobserve(observer);
<a name="line57"></a> *   };
<a name="line58"></a> *
<a name="line59"></a> *   ClassA.prototype.notify = function(opt_data) {
<a name="line60"></a> *     this.observable_.notify(opt_data);
<a name="line61"></a> *   };
<a name="line62"></a> * &lt;/pre&gt;
<a name="line63"></a> *
<a name="line64"></a> * @param {!goog.labs.observe.Observable=} opt_actualObservable
<a name="line65"></a> *     Optional observable object. Defaults to &#39;this&#39;. When used as
<a name="line66"></a> *     base class, the parameter need not be given. It is only useful
<a name="line67"></a> *     when using this class to implement implement {@code Observable}
<a name="line68"></a> *     interface on another object, see example above.
<a name="line69"></a> * @constructor
<a name="line70"></a> * @implements {goog.labs.observe.Observable}
<a name="line71"></a> * @extends {goog.Disposable}
<a name="line72"></a> */
<a name="line73"></a>goog.labs.observe.SimpleObservable = function(opt_actualObservable) {
<a name="line74"></a>  goog.base(this);
<a name="line75"></a>
<a name="line76"></a>  /**
<a name="line77"></a>   * @type {!goog.labs.observe.Observable}
<a name="line78"></a>   * @private
<a name="line79"></a>   */
<a name="line80"></a>  this.actualObservable_ = opt_actualObservable || this;
<a name="line81"></a>
<a name="line82"></a>  /**
<a name="line83"></a>   * Observers registered on this object.
<a name="line84"></a>   * @type {!Array.&lt;!goog.labs.observe.Observer&gt;}
<a name="line85"></a>   * @private
<a name="line86"></a>   */
<a name="line87"></a>  this.observers_ = [];
<a name="line88"></a>};
<a name="line89"></a>goog.inherits(goog.labs.observe.SimpleObservable, goog.Disposable);
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/** @override */
<a name="line93"></a>goog.labs.observe.SimpleObservable.prototype.observe = function(observer) {
<a name="line94"></a>  goog.asserts.assert(!this.isDisposed());
<a name="line95"></a>
<a name="line96"></a>  // Registers the (type, observer) only if it has not been previously
<a name="line97"></a>  // registered.
<a name="line98"></a>  var shouldRegisterObserver = !goog.array.some(this.observers_, goog.partial(
<a name="line99"></a>      goog.labs.observe.Observer.equals, observer));
<a name="line100"></a>  if (shouldRegisterObserver) {
<a name="line101"></a>    this.observers_.push(observer);
<a name="line102"></a>  }
<a name="line103"></a>  return shouldRegisterObserver;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/** @override */
<a name="line108"></a>goog.labs.observe.SimpleObservable.prototype.unobserve = function(observer) {
<a name="line109"></a>  goog.asserts.assert(!this.isDisposed());
<a name="line110"></a>  return goog.array.removeIf(this.observers_, goog.partial(
<a name="line111"></a>      goog.labs.observe.Observer.equals, observer));
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/** @override */
<a name="line116"></a>goog.labs.observe.SimpleObservable.prototype.notify = function(opt_data) {
<a name="line117"></a>  goog.asserts.assert(!this.isDisposed());
<a name="line118"></a>  var notice = new goog.labs.observe.Notice(this.actualObservable_, opt_data);
<a name="line119"></a>  goog.array.forEach(
<a name="line120"></a>      goog.array.clone(this.observers_), function(observer) {
<a name="line121"></a>        observer.notify(notice);
<a name="line122"></a>      });
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/** @override */
<a name="line127"></a>goog.labs.observe.SimpleObservable.prototype.disposeInternal = function() {
<a name="line128"></a>  this.observers_.length = 0;
<a name="line129"></a>};
</pre>


</body>
</html>
